package com.example.courseapp.activity;

import static com.example.courseapp.utils.TimeUtils.getSafeDateFormat;
import static com.example.courseapp.utils.TimeUtils.isToday;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.TimeUtils;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.courseapp.R;
import com.example.courseapp.RetrofitHelper;
import com.example.courseapp.adapter.FoodRecordAdapter;
import com.example.courseapp.bean.ChildDietaryRecords;
import com.example.courseapp.bean.FoodInfo;
import com.example.courseapp.net.CourseService;
import com.example.courseapp.utils.RxUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ChildFoodActivity extends AppCompatActivity {

    Button back, foodTuijian;

    RecyclerView recyclerView;

    CourseService service;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_child_fodd);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        service = RetrofitHelper.getInstance().createService(CourseService.class);

        Integer childID = getIntent().getIntExtra("childID", 0);

        FoodRecordAdapter foodRecordAdapter = new FoodRecordAdapter();

        back = findViewById(R.id.back);

        foodTuijian = findViewById(R.id.foodTuijian);

        foodTuijian.setOnClickListener(v -> {
            startActivity(new Intent(this, RecordActivity.class).putExtra("childID", childID));
        });

        back.setOnClickListener(v -> {
            finish();
        });

        recyclerView = findViewById(R.id.recycler);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(foodRecordAdapter);

        service.getDietaryList(childID).enqueue(new Callback<List<ChildDietaryRecords>>() {
            @Override
            public void onResponse(Call<List<ChildDietaryRecords>> call, Response<List<ChildDietaryRecords>> response) {
                List<ChildDietaryRecords> data = response.body();
                if (data != null && data.size() > 0) {
                    RxUtil.observe(Schedulers.newThread(), Observable.create(e -> {
                        List<ChildDietaryRecords> xData = new ArrayList<>();
                        for (int x = 0; x < data.size(); x++) {
                            ChildDietaryRecords records = data.get(x);
                            boolean isToady = isToday(records.getCreateTime(), getSafeDateFormat("yyyy-MM-dd HH:mm:ss"));
                            if (isToady) {
                                xData.add(records);
                            }
                        }
                        for (int x = 0; x < xData.size(); x++) {
                            ChildDietaryRecords records = xData.get(x);
                            List<FoodInfo> newData = new ArrayList<>();
                            List<FoodInfo> infoList = new Gson().fromJson(records.getFood(), new TypeToken<List<FoodInfo>>() {
                            }.getType());
                            for (int i = 0; i < infoList.size(); i++) {
                                boolean isExist = false;
                                for (int j = 0; j < newData.size(); j++) {
                                    if (newData.get(j).getId() == infoList.get(i).getId()) {
                                        isExist = true;
                                        break;
                                    }
                                }
                                if (!isExist)
                                    newData.add(infoList.get(i));
                            }
                            records.newFooldList = newData;
                        }
                        e.onNext(xData);
                        e.onComplete();
                    })).subscribe(o1 -> {
                        foodRecordAdapter.bindData((List<ChildDietaryRecords>) o1);
                    });
                }
            }

            @Override
            public void onFailure(Call<List<ChildDietaryRecords>> call, Throwable t) {

            }
        });


    }
}